Fix union, intersection, and difference operations with tree fields #96
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes an issue where using union, intersection, or difference operations on querysets with tree fields would raise errors. The problem manifested in two ways:
TreeQuery.get_compiler() takes from 1 to 3 positional arguments but 4 were given
Cannot resolve keyword 'tree_depth' into field
Root Cause
Tree fields like
tree_depth
,tree_path
, andtree_ordering
are computed fields generated via Common Table Expressions (CTEs) and are not actual database columns. Django's combinator SQL generation for union/intersection/difference operations requires all queries to have matching field structures, but tree fields cannot be resolved as regular model fields.Solution
The fix implements automatic detection of combinator operations and falls back to regular SQL compilation without tree fields:
TreeQuery.get_compiler()
to accept theelide_empty
parameter that Django's combinator logic passesTreeQuery
is part of a combinator operation, it automatically compiles as a regular query without CTE generationUsage
Behavior
tree_depth
,tree_path
, etc.)This is a backward-compatible change that adds new functionality without breaking existing code.
Fixes #55.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.